package com.grace.cms.general.controller.general;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.grace.cms.common.domain.entity.CmsLookRecord;
import com.grace.cms.common.enums.RecordTypeEnums;
import com.grace.cms.general.controller.CommonController;
import com.grace.cms.general.service.impl.GeneralRecordServiceImpl;
import com.grace.common.core.domain.AjaxResult;
import com.grace.common.utils.ServletUtils;
import com.grace.common.utils.ip.IpUtils;

import eu.bitwalker.useragentutils.UserAgent;


@Controller
@RequestMapping("/{themeName}/general/record")
public class GeneralRecordController extends CommonController {
    @Autowired
    private GeneralRecordServiceImpl recordService;

    /**
     * 文章点击记录
     * 
     * @param cmsLookRecord
     * @return
     */
    @PostMapping("/{type}/addLookRecord")
    @ResponseBody
    public AjaxResult addLookRecord(@PathVariable("themeName") String themeName,@PathVariable(value = "type") RecordTypeEnums type,@RequestBody CmsLookRecord cmsLookRecord) {
        setCommonRecordParam(cmsLookRecord, themeName, type,true);
        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        String ip = IpUtils.getIpAddr();
        cmsLookRecord.setIp(ip);
        cmsLookRecord.setUserSystem(userAgent.getOperatingSystem().getName());
        cmsLookRecord.setUserBower(userAgent.getBrowser().getName());
        return AjaxResult.success(recordService.insertCmsLookRecord(cmsLookRecord));
    }

}
